public class BinarySearchTree {

    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }
    }

    public TreeNode root;

    /**
     * 插入一个元素
     *
     * @param key
     */
    public boolean insert(int key) {
        TreeNode newnode = new TreeNode(key);
        if (root == null) {
            root = newnode;
            return true;
        }
        TreeNode parent = null;
        TreeNode cur = root;
        while (cur != null) {
            if (cur.key > key) {
                parent = cur;
                cur = cur.left;
            } else {
                parent = cur;
                cur = cur.right;
            }
        }
        if (parent.key > key) {
            parent.left = newnode;
        } else if (parent.key < key) {
            parent.right = newnode;
        } else {
            return false;
        }
        return true;
    }

    //查找key是否存在
    public boolean search(int key) {
        if (root == null) {
            return false;
        }
        TreeNode cur = root;
        while (cur != null) {
            if (cur.key > key) {
                cur = cur.left;
            } else if ((cur.key < key)) {
                cur = cur.right;
            } else {
                return true;
            }
        }
        return false;
    }

    //删除key的值
    public void remove(int key) {
        if (root == null) {
            return;
        }
        TreeNode parent = null;
        TreeNode cur = root;
        while (cur != null) {
            if (cur.key > key) {
                parent = cur;
                cur = cur.left;
            } else if (cur.key < key){
                parent = cur;
                cur = cur.right;
            }else {
                if (cur.left == null) {
                  if (cur == root) {
                      root = cur.right;
                  }else {
                      if (parent.left == cur) {
                          parent.left = cur.right;
                      }else {
                          parent.right = cur.right;
                      }
                  }
                }else if (cur.right == null) {
                    if (cur == root) {
                        root = cur.left;
                    }else {
                        if (parent.left == cur) {
                            parent.left = cur.left;
                        }else {
                            parent.right = cur.left;
                        }
                    }
                }else {
                    TreeNode tmp = cur;
                    parent = cur;
                    cur = cur.right;
                    if (cur.left == null) {
                        tmp.key = cur.key;
                        parent.right = cur.right;
                        return;
                    }
                    while (cur.left != null) {
                        parent = cur;
                        cur = cur.left;
                    }
                    tmp.key = cur.key;
                    parent.left = cur.right;
                }
            }
        }
    }
}